package com.mjfeed.controller.m.member;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mjfeed.controller.BaseController;
import com.mjfeed.entity.Member;
import com.mjfeed.service.MemberService;
import com.mjfeed.service.OrderService;
import com.mjfeed.util.Md5Util;
import com.mjfeed.util.WebUtils;

/**
 * @description 会员
 * @author zhaominglei
 * @date 2014年10月7日
 */
@Controller("mMemberController")
@RequestMapping("/m/member")
public class MemberController extends BaseController {
	
	@Resource(name = "memberService")
	private MemberService memberService;
	@Resource(name = "orderService")
	private OrderService orderService;
	
	/**
	 * 首页
	 */
	@RequestMapping(value = "/index", method=RequestMethod.GET)
	public String index(HttpServletRequest request, HttpServletResponse response, Model model){
		Member member = memberService.getCurrent();
		if (member == null || member.getId() == null || member.getId() < 1) {
			return ERROR_VIEW;
		}
		request.setAttribute("member", member);
		return "m/member/index";
	}
	
	@RequestMapping(value = "/edit",method=RequestMethod.GET)
	public String edit(HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("member", memberService.getCurrent());
		return "m/member/edit";
	}
	
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	public String update(Member member, HttpServletRequest request, HttpServletResponse response, Model model){
		Member pMember = memberService.getById(Member.class, member.getId());
		pMember.setEmail(member.getEmail());
		pMember.setName(member.getName());
		pMember.setMobile(member.getMobile());
		memberService.update(pMember);
		return "m/member/edit";
	}
	
	@RequestMapping(value = "/editpassword", method=RequestMethod.GET)
	public String editpassword(HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("member", memberService.getCurrent());
		return "m/member/editpassword";
	}
	
	@RequestMapping(value = "/updatepassword", method=RequestMethod.POST)
	public String updatepassword(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		String pPassword = request.getParameter("pPassword");
		String nPassword = request.getParameter("nPassword");
		String cfnPassword = request.getParameter("cfnPassword");
		if (StringUtils.isEmpty(pPassword) || StringUtils.isEmpty(nPassword) || StringUtils.isEmpty(cfnPassword)) {
			return ERROR_VIEW;
		}
		if (!cfnPassword.equals(nPassword)) {
			return ERROR_VIEW;
		}
		Member pMember = memberService.getById(Member.class, id);
		if (!Md5Util.encrypt(pPassword).equals(pMember.getPassword())) {
			return ERROR_VIEW;
		}
		pMember.setPassword(Md5Util.encrypt(nPassword));
		memberService.update(pMember);
		request.getSession().removeAttribute(Member.PRINCIPAL_ATTRIBUTE_NAME);
		WebUtils.removeCookie(request, response, Member.USERNAME_COOKIE_NAME);
		return "redirect:/m/login.shtml";
	}
}
